
use GDBM_File;
@lines = <STDIN>;
chomp(@lines);
$num = @lines;

%hash_srctgt = ();
%hash = ();

for($i = 0; $i < $num; $i++)
{
	if($lines[$i] !~ /^\s*$/)
	{
		($tmp,$count,$src,$tgt) = split(/\s+/,$lines[$i]);
#		print $count."  -->  ".$src."  -->  ".$tgt."\n";
		if($src eq "")
		{
			$src = "NULL";
		}
		if($tgt eq "")
		{
			$tgt = "NULL";
		}
		$src_tgt = $src."____".$tgt;
		$hash_srctgt{$src_tgt}+=$count;
		$hash{$src}+=$count;
	}
}

foreach $key (sort keys %hash_srctgt) 
{
	($src,$tgt) = split(/____/,$key);
#	print $key."\t".$hash{$src}."\n";
	$prob = $hash_srctgt{$key}/$hash{$src};
	$hash_main{ $key } = $prob;
}

%count = ();
%hash = ();
foreach $key (sort {$hash_main{$b} <=> $hash_main{$a}} keys(%hash_main))
{
	($src,$tgt) = split(/____/,$key);
	if($hash{ $src } eq "")
	{
		$hash{ $src } = $tgt;
		$count{ $src }= 1;
	}
	else
	{
		if(!$ARGV[1] || $count{ $src } < $ARGV[1])
		{
			$hash{ $src } = $hash{ $src }."___".$tgt;
			$count{ $src }++;
		}
	}
}

tie(%LEX,GDBM_File,$ARGV[0],&GDBM_WRCREAT,0644);

foreach $key (keys %hash)
{
	$value = $hash{ $key };
#	$value =~ s/\s+/_/g;
	print $key."  --> ".$value."\n"; 
	$LEX{$key} = $value;
}
